package homework8;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.Iterator;
import java.util.LinkedList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;

import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
//
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Translation extends JFrame {

	private JPanel contentPane;
	private JTextField rawString;
	private JButton btnTsl;
	private JTextArea tslArea;
	private Connection con;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Translation frame = new Translation();
					frame.setVisible(true);
				}
				catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Translation() {
		createContents();
	}
	
	private void handle(String tmp) {
		if(tmp.isEmpty()) {
			tslArea.setText("不存在");
		}
		else {
			tslArea.setText(tmp);
		}
	}
	private void createContents() {
		//
		
		//
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		rawString = new JTextField();
		rawString.setBounds(87, 20, 213, 21);
		contentPane.add(rawString);
		rawString.setColumns(10);
		
		btnTsl = new JButton("Translate it.");
		//tslArea.setText("中文字符需要2字或者以上");
		btnTsl.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					Class.forName("com.mysql.jdbc.Driver");
					//System.out.println(222);			
					String url,user,password;
					url="jdbc:mysql://localhost:3306/Student?useUnicode=true&characterEncoding=utf8";
					user="root";
					password="password";
					
					con=DriverManager.getConnection(url, user, password);
					//System.out.println(123);
					PreparedStatement sql;
					String regexEng="[a-zA-Z]+";
					String regexChn="[\\u4e00-\\u9fa5]{2,}";
					String raw=rawString.getText();
					if(raw.matches(regexChn)) {
						tslArea.setText("");
						sql=con.prepareStatement("Select * From dic Where chn LIKE ?");
						sql.setString(1, "%"+raw+"%");
						ResultSet rs=sql.executeQuery();
						StringBuffer tmp=new StringBuffer();
						while(rs.next()) {
							tmp.append(rs.getString(2)+"\r\n"+rs.getString(1)+"\r\n"+"\r\n");
							
							
							
						}
						handle(tmp.toString());
						
					}
					else if(raw.matches(regexEng)) {
						sql=con.prepareStatement("Select * From dic Where eng = ?");
						raw=raw.toLowerCase();
						sql.setString(1, raw);
						ResultSet rs=sql.executeQuery();
						StringBuffer tmp=new StringBuffer();
						while(rs.next()) {
							tmp.append(rs.getString(1)+"\r\n"+"\r\n");
						}
						handle(tmp.toString());
						
					}
					else {
						tslArea.setText("格式不对（中文需要2字以上）");
					}
					
					
				}
				catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				catch (SQLException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
				
			}
		});
		btnTsl.setBounds(87, 51, 213, 23);
		contentPane.add(btnTsl);
		
		tslArea = new JTextArea();
		JScrollPane scr=new JScrollPane(tslArea);
		scr.setBounds(87, 100, 213, 150);
		
		scr.setHorizontalScrollBarPolicy(
		JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		scr.setVerticalScrollBarPolicy(
		JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		//tslArea.setBounds(87, 155, 213, 78);
		//contentPane.add(tslArea);
		contentPane.add(scr);
		tslArea.setText("中文需要2字及以上");
	
		
	}
}
